<?php
class ModelCustomerCustomerBlacklist extends Model {
	public function getCustomerBlacklists($data = array()) {
		$sql = "SELECT cb.customer_id, cb.reason, FROM_UNIXTIME(cb.date_added, '%Y-%m-%d') date_added, " . 
			"CONCAT(c.firstname, ' ', c.lastname) name, c.email FROM " . 
			DB_PREFIX . "customer_blacklist cb LEFT JOIN " . 
			DB_PREFIX . "customer c on cb.customer_id = c.customer_id";

		if (isset($data['filter_email'])) {
			$sql .= " WHERE LCASE(c.email) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_email'])) . "%'";
		}
		
		$sql .= " ORDER BY cb.date_added";	
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalCustomerBlacklists($data = array()) {
		$sql = "SELECT COUNT(c.customer_id) AS total FROM " . 
			DB_PREFIX . "customer_blacklist cb LEFT JOIN " . 
			DB_PREFIX . "customer c on cb.customer_id = c.customer_id";

		if (isset($data['filter_email'])) {
			$sql .= " WHERE LCASE(c.email) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_email'])) . "%'";
		}
		
      	$query = $this->db->query($sql);
				 
		return $query->row['total'];
	}
}
?>